﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;

namespace NET
{
    public class NetService
    {
        private HttpWebRequest m_Request = null;
        private HttpWebResponse m_Response = null;

        public NetService(String url, CookieContainer co)
        {
            m_Request = (HttpWebRequest)HttpWebRequest.Create(url);
            m_Request.Accept = "*/*";
            m_Request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; InfoPath.2; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; TheWorld)";
            m_Request.KeepAlive = false;

            if (co != null)
            {
                m_Request.CookieContainer = co;
            }
        }

        public String Get(String encoding)
        {
            try
            {
                m_Request.Method = "GET";
                m_Response = (HttpWebResponse)m_Request.GetResponse();
                StreamReader sr = new StreamReader(m_Response.GetResponseStream(), System.Text.Encoding.GetEncoding(encoding));
                return sr.ReadToEnd();
            }
            catch (Exception)
            {
                return "";
            }
        }

        public String Get(String encoding, String referer)
        {
            try
            {
                m_Request.Method = "GET";
                m_Request.Referer = referer;
                m_Response = (HttpWebResponse)m_Request.GetResponse();
                StreamReader sr = new StreamReader(m_Response.GetResponseStream(), System.Text.Encoding.GetEncoding(encoding));
                return sr.ReadToEnd();
            }
            catch (Exception)
            {
                return "";
            }
        }

        public Stream GetStream(String referer)
        {
            try
            {
                m_Request.Method = "GET";
                m_Request.Referer = referer;
                m_Response = (HttpWebResponse)m_Request.GetResponse();
                return m_Response.GetResponseStream();
            }
            catch (Exception)
            {
                return null;
            }
        }

        public void AddHeader(String name, String value)
        {
            m_Request.Headers.Add(name, value);
        }

        public String Post(String par, String encoding, String referer)
        {
            try
            {
                byte[] param = Encoding.ASCII.GetBytes(par);

                m_Request.Method = "POST";
                m_Request.ContentType = "application/x-www-form-urlencoded";
                m_Request.ContentLength = param.Length;
                m_Request.Referer = referer;
                using (Stream reqStream = m_Request.GetRequestStream())
                {
                    reqStream.Write(param, 0, param.Length);
                }

                m_Response = (HttpWebResponse)m_Request.GetResponse();
                StreamReader sr = new StreamReader(m_Response.GetResponseStream(), System.Text.Encoding.GetEncoding(encoding));
                return sr.ReadToEnd();
            }
            catch (Exception)
            {
                return "";
            }
        }

        public String Post(String par, String encoding, String referer, String header, String contenttype)
        {
            try
            {
                byte[] param = Encoding.ASCII.GetBytes(par);

                m_Request.Method = "POST";
                m_Request.ContentType = contenttype;
                m_Request.ContentLength = param.Length;
                m_Request.Referer = referer;
                m_Request.Headers.Add(header);

                using (Stream reqStream = m_Request.GetRequestStream())
                {
                    reqStream.Write(param, 0, param.Length);
                }

                m_Response = (HttpWebResponse)m_Request.GetResponse();
                StreamReader sr = new StreamReader(m_Response.GetResponseStream(), System.Text.Encoding.GetEncoding(encoding));
                return sr.ReadToEnd();
            }
            catch (Exception)
            {
                return "";
            }
        }

        public CookieCollection Cookies
        {
            get
            {
                return m_Response.Cookies;
            }
        }
    }
}
